//原生canvas的dom
const mcanvas = document.getElementById('main-canvas');
//fabric canvas对象
let canvas = null;
//橡皮擦、描边工具按下效果
const effect = new fabric.Circle({
    radius: 10,
    fill: 'rgba(100,100,100,0.5)',
    top: -20
});
//当前页号指示
const currentPageNumber = $('#current-page');
//总页数指示
const totalPageNumber = $('#total-page');
//底部文档名指示
const fileName = $('#file-name');
//调色板颜色按钮
const palette = $('#palette');
//状态栏信息
const status = $('#status');

function getCanvasJson() {
    canvas.remove(effect);
    let data = JSON.stringify(canvas.toJSON());
    canvas.add(effect);
    return data;
}

//将json渲染到canvas上
function renderJson(json) {
    canvas.clear();
    canvas.loadFromJSON(json, function () {
        //填充背景，否则导出图片为黑色背景
        canvas.setBackgroundColor('#fcfcfc');
        canvas.add(effect);
    })
}

function showInfo(info) {
    $('#info-panel-content').text(info).parent().parent()
        .fadeIn(300).delay(1600).fadeOut(800);
}

function closeInfo() {
    $('#info-panel').hide();
}

function changeInfo(info) {
    $('#info-panel-content').text(info);
}

function getAjaxErrorMsg(o) {
    if(o.status === 401){
        user.logged = false;
        $.cookie('token', '');
        $('#files-fragment').hide();
        $('#user-fragment').hide();
    }
    if(o.responseText){
        try{
            return JSON.parse(o.responseText).msg;
        }catch (e){
            return "服务器出了点小问题";
        }
    }else {
        return "网络故障，无法连接服务器"
    }
}

function docLoading() {
    $('#doc-loading').show();
}

function docLoaded() {
    $('#doc-loading').hide();
}

//让docElm对象全屏
function requestFullscreen(docElm) {
    let method = docElm.requestFullscreen ||
        docElm.msRequestFullscreen ||
        docElm.mozRequestFullScreen ||
        docElm.webkitRequestFullScreen;
    method.call(docElm);
    console.log("enter full");
}

//退出全屏
function exitFullscreen() {
    let method = document.exitFullscreen ||
        document.mozCancelFullScreen ||
        document.webkitCancelFullScreen ||
        document.msExitFullscreen;
    method.call(document);
    console.log("exit full");
}

//获取全屏对象
function fullscreenElement() {
    return (document.fullscreenElement ||
        document.msFullscreenElement  ||
        document.mozFullScreenElement ||
        document.webkitFullscreenElement || null);
}
